<?php
class Landing_ads extends Controller {
    
    function Landing_ads(){    
        parent::Controller();
        $this->load->helper(array('form','html'));    
        $this->load->model(array('Landing_ads_model','User_admin_model','Network_model'));         
        $this->load->library('form_validation');
    }
    
    function index(){
        if($this->session->userdata('admin') != 'admin'){
            redirect('admin');
        }
        
        if($this->input->post('submit')){
            $message    = $this->input->post('message');
            $url        = $this->input->post('url');
            $alt_tag    = $this->input->post('alt');
            $upload     = $this->input->post('upload');
            $items      = $this->input->post('show_item');
            $check_update = $this->Landing_ads_model->count_landing_ads();
            // update exist landing ads
            if(!empty($check_update)){
                for($i=0; $i < $check_update; $i++){
                    $data['query'] .= $this->Landing_ads_model->update_landing_ads($message[$i], $url[$i], $alt_tag[$i], $upload[$i], $i+1);
                }
            }
            // if no to update set to zero
            if(empty($check_update)){
                $check_update = 0;    
            }
            // add landing ads after update or just insert            
            for($j = $check_update; $j < $items; $j++){
                $data['query'] .= $this->Landing_ads_model->add_landing_ads($message[$j], $url[$j], $alt_tag[$j], $upload[$j]);
            }
            $data['check_update'] = $check_update; 
            $data['items'] = $items;  
        }
        $count = $this->Landing_ads_model->count_landing_ads();
        $data['ads_list'] = $this->Landing_ads_model->get_landing_ads($count,0);
        $data['total_network'] = $this->Network_model->total_network();
        $data['count'] = $count;
        //echo '<pre><font color="#999999">';print_r($data); echo '</font></pre>';
        //echo '<pre><font color="#999999">';print_r($_POST); echo '</font></pre>';
        $this->load->view(TEMPLATE_NAME.'/admin/landing_ads/landing_ads',$data);    
    }
    
}
?>
